# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class Spider0925Pipeline:
    # 初始化数据库参数
    def __init__(self):
        self.conn = pymysql.connect(host='localhost', port=3306,
                                    user='root', password='root123',
                                    database='spider', charset='utf8mb4')  # 随便吧，反正能登录,看官请不要乱动哦~
        self.cursor = self.conn.cursor()

    def close_spider(self, spider):
        self.conn.commit()
        self.conn.close()

    def process_item(self, item, spider):
        title = item.get('title', '')
        score = item.get('score', 0)
        main_idea = item.get('main_idea', '')
        self.cursor.execute(
            'INSERT INTO db_movie(movie_title,movie_score,movie_idea) VALUE(%s,%s,%s)', (title, score, main_idea)
        )
        return item
